package com.tom.AIAnswer.ai;

import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage;
import com.zhipu.oapi.service.v4.model.ChatMessageRole;
import com.zhipu.oapi.service.v4.model.ModelApiResponse;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
public class ZhipuAI {
    private static final String API_KEY = "361777a6d6ad3fa916158bc8b7a9e8c2";
    private static final String SECRET_KEY = "p8zPxxt6P3tYovrN";
    private static final ClientV4 client = new ClientV4.Builder(API_KEY+"."+SECRET_KEY).build();

    @Test
    public void test() {

        /**
         * 同步调用
         */
            //初始化聊天消息列表
            List<ChatMessage> messages = new ArrayList<>();
            //初始化聊天消息
            ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), "根据2024奥运会写一句歌词");
            messages.add(chatMessage);
            //初始化请求id，不指定请求id就会自动生成
//            String requestId = String.format(requestIdTemplate, System.currentTimeMillis());

        //调用模型接口
            ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                    .model(Constants.ModelChatGLM4)
                    .stream(Boolean.FALSE)
                    .invokeMethod(Constants.invokeMethod)
                    .messages(messages)
//                    .requestId(requestId)
                    .build();
            ModelApiResponse invokeModelApiResp = client.invokeModelApi(chatCompletionRequest);

        System.out.println("model output:" + invokeModelApiResp.getData().getChoices().get(0).toString());


    }

}
